/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 */
/*
 * Created by Rieu Damien (http://totalprog.blogspot.com/)
 *
 */
package fr.dr.monitor.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.apache.log4j.*;

import fr.dr.monitor.model.User;
import fr.dr.monitor.repository.IUser;


@Controller
public class MainController  {

	 //Logger
	 private static final Logger logger = Logger.getLogger(MainController.class);
	 
	 private IUser userDAO;
	 
	 @Autowired	 
	 public MainController(IUser user) {
		userDAO = user;
	 }
	 
	 /**
	    * HandlerGet Handler method for URL /. 
	    * 
	    * @param name view name.
	    * @param model Data map we will use in the view. 
	    * @return view to use.
	    */
	  @RequestMapping(value="/")
	  public  String toIndex(
	    @RequestParam(value="name",required=false) String name, 
	    ModelMap model) 
	  {
		logger.debug(">toIndex()");  
	    model.addAttribute("name",name);
	    // In this example we will use /WEB-INF/jsp/index.jsp.
	    return "index";
	  }
	  
	  /**
	   * List all the users and display "users.jsp" page.
	   * The location and mime-type of "users.jsp" will be resolved by Spring container's "viewResolver" bean
	   * that declared in "integration-mvc.xml"
	   */
	  @RequestMapping(value = "users", method = RequestMethod.GET)
	  public ModelAndView getUsers() {
	    List<User> users = userDAO.getUsers();
	    ModelAndView modelAndView = new ModelAndView("users");
	    modelAndView.addObject("users", users);
	    return modelAndView;
	  }	  
}